var advice = {

	init : function()
	{
		advice.make($('#balance'));
		advice.check($('#check'));
	},

	make : function($obj)
	{
		$obj.click(function(){
<<<<<<< HEAD
			tool.customAlert({'obj':'结算后不能修改，确定结算？','confirm':true}, 'text', function(){
				var url = site.url + 'advice/make';
				tool.customAlert({'obj':'结算中...请等候...'});
				style.loading('start');
				$.get(url, function(data){
					j = tool.toJson(data);
					style.loading('end');
					if ('DONE' == j.r)
					{
						tool.customAlert({'obj':j.d + ' 3秒后刷新'});
						setTimeout(function(){
							location.href = site.url + 'advice/stock/list';
						}, 3000);
					}
					else
					{
						tool.customAlert({'obj':j.d});
					}
				});
=======
			var url = site.url + 'advice/make';
			tool.customAlert({'obj':'结算中...请等候...'});
			style.loading('start');
			$.get(url, function(data){
				j = tool.toJson(data);
				style.loading('end');
				if ('DONE' == j.r)
				{
					tool.customAlert({'obj':j.d + ' 3秒后刷新'});
					//setTimeout(function(){
					//	location.href = site.url + 'advice/stock/list';
					//}, 3000);
				}
				else
				{
					tool.customAlert({'obj':j.d});
				}
>>>>>>> ff5f7ef6de592146b376adc8ed0c004b06507151
			});
		});
	},

	check : function($obj)
	{
		$obj.click(function(){
			var url = site.url + 'advice/check';
			tool.customAlert({'obj':'检查中...请等候...'});
			style.loading('start');
			$.get(url, function(data){
				j = tool.toJson(data);
				style.loading('end');
				if ('DONE' == j.r)
				{
					tool.customAlert({'obj':j.d});
				}
				else
				{
					tool.customAlert({'obj':j.d});
				}
			});
		});
	},

	stock : {

		balance : function($obj)
		{
			$obj.click(function(){
<<<<<<< HEAD
				tool.customAlert({'obj':'结算后不能修改，确定结算？','confirm':true}, 'text', function(){
					var url = site.url + 'advice/make';
					tool.customAlert({'obj':'结算中...请等候...'});
					style.loading('start');
					$.get(url, function(data){
						j = tool.toJson(data);
						style.loading('end');
						if ('DONE' == j.r)
						{
							tool.customAlert({'obj':j.d + ' 3秒后刷新'});
							setTimeout(function(){
								location.href = site.url + 'advice/stock/list';
							}, 3000);
						}
						else
						{
							tool.customAlert({'obj':j.d});
						}
					});
=======
				var url = site.url + 'advice/make';
				tool.customAlert({'obj':'结算中...请等候...'});
				style.loading('start');
				$.get(url, function(data){
					j = tool.toJson(data);
					style.loading('end');
					if ('DONE' == j.r)
					{
						tool.customAlert({'obj':j.d + ' 3秒后刷新'});
						setTimeout(function(){
							location.href = site.url + 'advice/stocks/';
						}, 3000);
					}
					else
					{
						tool.customAlert({'obj':j.d});
					}
>>>>>>> ff5f7ef6de592146b376adc8ed0c004b06507151
				});
			});
		},

		view : {

			init : function(){
				advice.stock.view.count_quantity();
				advice.stock.view.show_stock_quantity();
				advice.stock.view.init_stock();
				advice.stock.view.count_sub_goods_value();
				tool.set_fixed($('#general'));
				style.highline($('.main_goods'), '#eee');
				style.highline($('.each_sub_goods'), '#ccc');
			},

			count_quantity : function(){
				var $sq = $('.stock_quantity');
				$sq.each(function(){
					var $eq = $(this).children('.quantity');
					var $tq = $(this).prev('.total_quantity');
					var quantity = 0;
					$eq.each(function(){
						quantity += parseInt($(this).text());
					});
					$tq.text(quantity);
				});
			},

			show_stock_quantity : function(){
				$('.each_sub_goods').bind('mouseenter', function(){
					$this = $(this);
					tool.popup($(this).find('.stock_quantity'), $(this).find('.total_quantity'), [0, 50]);
					$('.stock_quantity').hide();
					$(this).find('.stock_quantity').show();
				})
				$('.each_sub_goods').mouseleave(function(){
					$(this).find('.stock_quantity').hide();
				})
			},

			count_sub_goods_value : function()
			{
				var $each_main = $('.main_goods');
				$each_main.each(function()
				{
					var $sub_goods_value = $(this).find('.sub_goods_value span');
					var sub_goods_value = 0;
					$(this).find('.each_sub_goods').each(function(){
						sub_goods_value += parseInt($(this).children('.price').text()) * parseInt($(this).children('.total_quantity').text());
					});
					$sub_goods_value.text(sub_goods_value + '￥');
				});
			},

			init_stock : function(){
				$('.stock_quantity .store').each(function(){
					var stock_id = $(this).text();
					if (1 == stock_id)
					{
						$(this).text('主仓');
					}
					else if (2 == stock_id)
					{
						$(this).text('副仓');
					}
					else
					{
						$(this).text('未知');
					}
				})
			}
		}
	},

	sale : {

		view : {

			init : function(){
				advice.sale.view.count_real_sale();
				tool.set_fixed($('#general'));
				style.highline($('.main_goods'), '#eee');
				style.highline($('.each_sub_goods'), '#ccc');
			},

			count_real_sale : function()
			{
				$main_goods = $('#details .main_goods');
				$main_goods.each(function(){
					$this_main = $(this);
					$sub_goods = $this_main.find('.each_sub_goods');
					$real_sale_element = $this_main.find('.real_sale_quantity span');
					real_quantity = 0;
					$sub_goods.each(function(){
						$this_sub = $(this);
						real_quantity += parseInt($this_sub.find('.sale_quantity').text()) - parseInt($this_sub.find('.return_quantity').text());
					});
					$real_sale_element.text(real_quantity);
				});
			}

		}

	}
}